package com.sansecy.video.player;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.Surface;

import com.sansecy.base.file.FileUtilsKt;

import java.io.IOException;

public class SystemMediaPlayer implements IPlayer {

    public static final int ACTION_NONE = 0;
    public static final int ACTION_PLAY = 1;
    public static final int ACTION_PAUSE = 2;
    public static final int ACTION_STOP = 3;
    private MediaPlayer mMediaPlayer;
    private Context mContext;
    private int mCurrentState;
    private boolean mIsPrepare;
    private int mAction;
    private int mBufferPercent;
    private onErrorListener mOnErrorListener;
    private Uri uri;

    public SystemMediaPlayer(Context context) {
        mMediaPlayer = new MediaPlayer();
        mContext = context;
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (mAction == ACTION_PLAY) {
                    mMediaPlayer.start();
                } else if (mAction == ACTION_PAUSE) {
                    mMediaPlayer.pause();
                } else if (mAction == ACTION_STOP) {
                    mMediaPlayer.stop();
                }
                mAction = ACTION_NONE;
            }
        });
        mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                mBufferPercent = percent;
            }
        });
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                if (mOnErrorListener != null) {
                    mOnErrorListener.onError(-1);
                }
                return true;
            }
        });
    }

    @Override
    public void setUri(Uri uri) {
        try {
            this.uri = uri;
            mMediaPlayer.setDataSource(mContext, uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setUri(Uri uri, String subtitleUrl) {
        try {
            mMediaPlayer.setDataSource(mContext, uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setVolume(float volume) {
        mMediaPlayer.setVolume(volume, volume);

    }

    @Override
    public long getDuration() {
        return mMediaPlayer.getDuration();
    }

    @Override
    public long getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    @Override
    public void seekTo(long position) {
        mMediaPlayer.seekTo((int) position);
    }

    @Override
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    @Override
    public void start() {
        if (mIsPrepare) {
            mMediaPlayer.start();
        } else {
            mAction = ACTION_PLAY;
        }
    }

    @Override
    public void pause() {
        if (mIsPrepare) {
            mMediaPlayer.pause();
        } else {
            mAction = ACTION_PAUSE;
        }
    }

    @Override
    public void stop() {
        if (mIsPrepare) {
            mMediaPlayer.stop();
        } else {
            mAction = ACTION_STOP;
        }
    }

    @Override
    public void release() {
        mIsPrepare = false;
        mMediaPlayer.release();
    }

    @Override
    public void setDisplay(Surface surface) {
        mMediaPlayer.setSurface(surface);
    }

    @Override
    public void prepareAsync() {
        try {
            mMediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setOnVideSizeChangeListener(OnVideoSizeChangedListener onVideoSizeChangedListener) {
        mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                if (onVideoSizeChangedListener != null) {
                    onVideoSizeChangedListener.onVideoSizeChanged(width, height);
                }
            }
        });
    }

    @Override
    public void setOnBufferListener(OnBufferListener onBufferListener) {
        mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                mBufferPercent = percent;
                if (onBufferListener != null) {
                    onBufferListener.onBufferUpdate(percent);
                }
            }
        });
    }

    @Override
    public void setOnCompletionListener(OnCompletionListener onCompletionListener) {
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (onCompletionListener != null) {
                    onCompletionListener.onCompletion();
                }
            }
        });
    }

    @Override
    public int getBufferPercentage() {
        return mBufferPercent;
    }

    @Override
    public void setSubtitle(String uri, String mimeType) {

    }

    @Override
    public Object getPlayer() {
        return mMediaPlayer;
    }

    @Override
    public IPlayer getIPlayer() {
        return this;
    }

    @Override
    public boolean isAudio() {
        return FileUtilsKt.getMimeType(this.uri.toString()).contains("audio");
    }

    @Override
    public void setOnErrorListener(onErrorListener onErrorListener) {
        mOnErrorListener = onErrorListener;
    }
}
